package thread.computer;

public class Customer implements Runnable{

	Computer com;
	int id;
	String name;
	
	public Customer(Computer com, int id, String name) {
		this.com = com;
		this.id = id;
		this.name = name;
	}
	
	@Override
	public void run() {
		while (true) {
			synchronized (com) {
				System.out.println("Customer " + id + " begin using the computer in 5h:");
				using5h();
				System.out.println("Customer " + name + " has fished his job");
				com.notifyAll();
				
				try {
					com.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void using5h() {
		for (int i=0; i<5; i++){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println((i+1) + "h ...");
		}
		
		com.timeInUse += 5;
		System.out.println("Time in use of the computer: " + com.timeInUse + "h");
		System.out.println();
	}

}
